/*
	Copyright 2010 Anatol Gregory Mayen
	
	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at
 
	http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/
package eu.maydu.gwt.gxtcomponents.client.password;

/**
 * Rules for defining the password strengths.
 * 
 * 
 * @author amayen
 *
 */
public class PasswordStrengthRule {
	
	private int ruleFulfilledPoints;
	private RuleClass ruleClass;
	private boolean ruleOptional;
	private int ruleFulfilledCount;

	public enum RuleClass {
		ALPHABET_IGNORECASE,
		ALPHABET_LOWERCASE,
		ALPHABET_UPPERCASE,
		NUMBERS,
		PASSWORD_LENGTH,
		SIGNS
	}
	
	private String ALPHABET_IGNORECASE_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIKLMNOPQRSTUVWXYZ";
	private String ALPHABET_UPPERCASE_CHARS = "ABCDEFGHIKLMNOPQRSTUVWXYZ";
	private String ALPHABET_LOWERCASE_CHARS = "abcdefghijklmnopqrstuvwxyz";
	private String NUMBERS_CHARS = "0123456789";
	private String SIGNS_CHARS = "!\"§$%&/()=?`´*'_:;{[]}\\~,.-#+";

	/**
	 * How many points does the rule add to the overall result
	 * if it was fulfilled
	 * @return
	 */
	public int getRuleFulfilledPoints() {
		return ruleFulfilledPoints;
	}

	/**
	 * Which type of input should this rule apply to.
	 * 
	 * ALPHABET_IGNORECASE	=	[A-Za-z]
	 * ALPHABET_LOWERCASE	=	[a-z]
	 * ALPHABET_UPPERCASE	=	[A-Z]
	 * NUMBERS				=	[0-9]
	 * PASSWORD_LENGTH		=	How long is the minimum password length
	 * SIGNS				=	[!"�$%&/()=?`�*'_:;{[]}\~,.-#+]
	 * 
	 * @return
	 */
	public RuleClass getRuleClass() {
		return ruleClass;
	}

	public String getRuleChars() {
		if(RuleClass.ALPHABET_IGNORECASE == ruleClass )
			return ALPHABET_IGNORECASE_CHARS;
		else if(RuleClass.ALPHABET_UPPERCASE == ruleClass)
			return ALPHABET_UPPERCASE_CHARS;
		else if(RuleClass.ALPHABET_LOWERCASE == ruleClass)
			return ALPHABET_LOWERCASE_CHARS;
		else if(RuleClass.NUMBERS == ruleClass)
			return NUMBERS_CHARS;
		else if(RuleClass.SIGNS == ruleClass)
			return SIGNS_CHARS;
		
		throw new RuntimeException("No rules class specified. Do a setRuleClass(ruleClass) first.");
	}
	/**
	 * Is the rule optional or must it be fulfilled under any 
	 * circumstances.
	 * 
	 * @return
	 */
	public boolean isRuleOptional() {
		return ruleOptional;
	}

	/**
	 * How many chars of the specified rule class must be at minimum present
	 * in the password for this rule to be validated as fulfilled.
	 * @return
	 */
	public int getRuleFulfilledCount() {
		return ruleFulfilledCount;
	}

	public void setRuleFulfilledPoints(int ruleFulfilledPoints) {
		this.ruleFulfilledPoints = ruleFulfilledPoints;
	}

	public void setRuleClass(RuleClass ruleClass) {
		this.ruleClass = ruleClass;
	}

	public void setRuleOptional(boolean ruleOptional) {
		this.ruleOptional = ruleOptional;
	}

	public void setRuleFulfilledCount(int ruleFulfilledCount) {
		this.ruleFulfilledCount = ruleFulfilledCount;
	}
	
	public boolean equals(Object o) {
		if(!(o instanceof PasswordStrengthRule))
			return false;
		return this.ruleClass.ordinal() == ((PasswordStrengthRule)o).ruleClass.ordinal();
	}

}
